package com.mfnote.util;

import java.io.File;
import java.io.FileOutputStream;

import com.mfnote.view.R;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.net.Uri;
import android.os.Environment;

public class ShareUtil {

	/**
	 * 分享图片和文字（用于记事本和涂鸦模块）
	 * 
	 * @param filepath
	 *            ：图片路径，如：/mnt/sdcard/MFNote/image/image_name.jpg
	 * @param content
	 *            ：记事本的文本内容
	 * @param context
	 */
//	public static void share(String filepath, String content, Context context) {
//		File f = new File(filepath);
//		Uri uri = Uri.fromFile(f);
//		Intent shareIntent = new Intent(Intent.ACTION_SEND);
//		if (uri != null&&filepath!=null&&filepath!="") {
//			shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
//			shareIntent.setType("image/jpeg");
//			// 当用户选择短信时使用sms_body取得文字
//			shareIntent.putExtra("sms_body", content);
//		} else {
//			shareIntent.setType("text/plain");
//		}
//		shareIntent.putExtra(Intent.EXTRA_TEXT, content);
//		// 自定义选择框的标题
//		// startActivity(Intent.createChooser(shareIntent, "邀请好友"));
//		// 系统默认标题
//		context.startActivity(shareIntent);
//	}

	/**
	 * 添加水印后再分享图片
	 */
	public static void share(String filepath, String content, Context context) {
		Intent shareIntent = new Intent(Intent.ACTION_SEND);
		if (filepath!=null&&filepath!="") {
			Bitmap bmpLogo = BitmapFactory.decodeResource(context.getResources(), R.drawable.bmp_logo);
			Bitmap mBitmap=BitmapFactory.decodeFile(filepath);
			//于水印图片合成
			Bitmap drawingBitmap = Bitmap.createBitmap(mBitmap.getWidth(),mBitmap.getHeight(), bmpLogo.getConfig());
			Canvas canvas = new Canvas(drawingBitmap);
	        Paint paint = new Paint();
	        canvas.drawBitmap(bmpLogo, mBitmap.getWidth()-bmpLogo.getWidth()-20, mBitmap.getHeight()-bmpLogo.getHeight()-20, paint);
	        paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_OVER));
	        canvas.drawBitmap(mBitmap, 0, 0, paint);
	        String imgpath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"mfnote.jpg";
	        File document = new File(imgpath);
			try {
				FileOutputStream fos=new FileOutputStream(document);
				drawingBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);// 把数据写入文件
			} catch (Exception e) {
				e.printStackTrace();
			}
			File f = new File(imgpath);
			Uri uri = Uri.fromFile(f);
			
			if(uri!=null){
				shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
				shareIntent.setType("image/jpeg");
				// 当用户选择短信时使用sms_body取得文字
				shareIntent.putExtra("sms_body", content);
			}
		} else {
			shareIntent.setType("text/plain");
		}
		shareIntent.putExtra(Intent.EXTRA_TEXT, content);
		// 自定义选择框的标题
		// startActivity(Intent.createChooser(shareIntent, "邀请好友"));
		// 系统默认标题
		context.startActivity(shareIntent);
	}
}
